package com.wteam.dragon.shop.controller;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wteam.dragon.shop.pojo.bean.Product;
import com.wteam.dragon.shop.pojo.bean.SkuStock;
import com.wteam.dragon.shop.pojo.dto.ProductDto;
import com.wteam.dragon.shop.pojo.dto.QueryProductDto;
import com.wteam.dragon.shop.service.ProductService;
import com.wteam.dragon.system.aop.log.log.Log;
import com.wteam.dragon.system.apps.security.annotation.AnonymousAccess;
import com.wteam.dragon.system.basebean.bean.ResultMessage;
import com.wteam.dragon.system.basebean.vo.PageVo;
import com.wteam.dragon.system.util.BeanUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author 陈晓辉
 * @Date 2020/7/7 20:30
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "商品")
@RequestMapping("product")
public class ProductController {
    private final ProductService productService;

    @Log("添加商品")
    @ApiOperation("添加商品")
    @PostMapping("addProduct")
    @PreAuthorize("@el.check('addProduct')")
    public ResultMessage delProductAttribute(@Validated ProductDto productDto){
        return productService.addProduct(productDto);
    }

    @Log("编辑SKU库存")
    @ApiOperation("编辑SKU库存")
    @PutMapping("editSkuStock")
    @PreAuthorize("@el.check('editSkuStock')")
    public ResultMessage editSkuStock(Long productId, String skuStockVoList){
        JSONArray jsonArray = JSONUtil.parseArray(skuStockVoList);
        List<SkuStock> skuStockList = JSONUtil.toList(jsonArray, SkuStock.class);
        return productService.editSkuStock(productId, skuStockList);
    }

    @Log("批量删除商品")
    @ApiOperation("批量删除商品")
    @DeleteMapping("delProduct")
    @PreAuthorize("@el.check('delProduct')")
    public ResultMessage delProduct(@RequestParam List<Long> ids){
        return productService.delProduct(ids);
    }

    @Log("分页获得商品列表")
    @ApiOperation("分页获得商品列表")
    @GetMapping("pageProduct")
    @AnonymousAccess
    public ResultMessage pageProduct(PageVo<Product> pageVo, QueryProductDto queryProductDto){
        Page<Product> page = BeanUtil.fromPage(pageVo);
        return productService.pageProduct(page, queryProductDto);
    }

    @Log("编辑商品")
    @ApiOperation("编辑商品")
    @PutMapping("editProduct")
    @PreAuthorize("@el.check('editProduct')")
    public ResultMessage editProduct(ProductDto productDto){
        return productService.editProduct(productDto);
    }

    @Log("通过商品id获得SKU库存")
    @ApiOperation("通过商品id获得SKU库存")
    @GetMapping("getSkuStockByProductId")
    @PreAuthorize("@el.check('getSkuStockByProductId')")
    public ResultMessage getSkuStockByProductId(Long productId, String skuCode){
        return productService.getSkuStockByProductId(productId, skuCode);
    }

    @Log("获得商品详情")
    @ApiOperation("获得商品详情")
    @GetMapping("getProduct")
    @PreAuthorize("@el.check('getProduct')")
    public ResultMessage getProduct(Long id){
        return productService.getProduct(id);
    }

    @Log("批量上架和下架")
    @ApiOperation("批量上架和下架")
    @PutMapping("editPublishStatus")
    @PreAuthorize("@el.check('editPublishStatus')")
    public ResultMessage editPublishStatus(@RequestParam List<Long> ids, Integer status){
        return productService.editPublishStatus(ids, status);
    }

    @Log("推荐和取消推荐")
    @ApiOperation("推荐和取消推荐")
    @PutMapping("editRecommendStatus")
    @PreAuthorize("@el.check('editRecommendStatus')")
    public ResultMessage editRecommendStatus(@RequestParam List<Long> ids, Integer status){
        return productService.editRecommendStatus(ids, status);
    }

    @Log("设为新品和取消新品")
    @ApiOperation("设为新品和取消新品")
    @PutMapping("editNewStatus")
    @PreAuthorize("@el.check('editNewStatus')")
    public ResultMessage editNewStatus(@RequestParam List<Long> ids, Integer status){
        return productService.editNewStatus(ids, status);
    }

    @Log("获得简单的商品信息")
    @ApiOperation("获得简单的商品信息")
    @PutMapping("pageSimpleProduct")
    @PreAuthorize("@el.check('pageSimpleProduct')")
    public ResultMessage pageSimpleProduct(PageVo<Product> pageVo, String name){
        Page<Product> page = BeanUtil.fromPage(pageVo);
        return productService.pageSimpleProduct(page, name);
    }
}
